/*
 * 文件名：BizcostApplyController.java
 * 版权：Copyright by www.infopub.top
 * 描述：
 * 修改人：wangw
 * 修改时间：2020年2月25日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.mgr.bpa.web;


import com.github.pagehelper.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import top.infopub.mgr.bpa.domain.BizcostApplyDomain;
import top.infopub.mgr.bpa.service.*;
import top.infopub.mgr.bpa.vo.BizcostApplyVo;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.ErrorCodeEnum;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.hr.service.StaffInfoService;
import top.infopub.mgr.ic.service.MonthCtrlService;
import top.infopub.mgr.ic.service.MonthCtrlSubpackService;
import top.infopub.mgr.util.DateUtils;
import top.infopub.mgr.util.FileHelper;
import top.infopub.mgr.util.LogUtil;
import top.infopub.mgr.util.LogUtil.AcctionType;
import top.infopub.mgr.util.LogUtil.LogModelType;
import top.infopub.model.bpa.BizcostApplyAtt;
import top.infopub.model.bpa.BizcostApplyDetail;
import top.infopub.model.bpa.ProContract;
import top.infopub.model.hr.StaffInfo;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;
import top.infopub.tdp.sys.service.CodeGeneratorConfigService;
import top.infopub.util.IdFactory;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.ParseException;
import java.util.*;


/**
 * @author wangw
 * @version 2020年2月25日
 * @see BizcostApplyController
 * @since
 */
@Controller
@RequestMapping("/bizcostApply")
public class BizcostApplyController extends GenericFlowController {

    @Autowired
    private BizcostApplyService bizcostApplyService;

    @Autowired
    private ProContractService proContractService;

    @Autowired
    private InnerAgrtService innerAgrtService;

    @Autowired
    private InnerAgrtRateService innerAgrtRateService;

    @Autowired
    private InnerSettleService innerSettleService;

    @Autowired
    private IncaeSureService incaeSureService;

    @Autowired
    private MonthCtrlService monthCtrlService;

    @Autowired
    private MonthCtrlSubpackService monthCtrlSubpackService;

    @Autowired
    private SubpackCotService subpackCotService;

    @Autowired
    private SubpackCotRateService subpackCotRateService;

    @Autowired
    private DeviceCotService deviceCotService;

    @Autowired
    private DeviceSureService deviceSureService;

    @Autowired
    private OtherCotService otherCotService;

    @Autowired
    private OtherSureService otherSureService;

    @Autowired
    private StaffInfoService staffInfoService;

    @Autowired
    private CodeGeneratorConfigService codeGeneratorConfigService;

    /**
     * 跳转列表页
     *
     * @return
     */
    @RequestMapping("/list")
    public String list(HttpServletRequest request) {
        return "bpa/lay-bizcostApply-list";
    }

    /**
     * 设备合同列表数据
     *
     * @return
     */
    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<BizcostApplyDomain> queryDataByCondition(BizcostApplyVo bizcostApplyVo,
                                                          ParamPage paramPage,
                                                          String monthStartDateStr,
                                                          String monthEndDateStr) {
        Page<BizcostApplyDomain> page = new Page<BizcostApplyDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        //查询时，结束日期增加一天
        if (bizcostApplyVo.getEndDate() != null) {
            bizcostApplyVo.setEndDate(DateUtils.addDays(bizcostApplyVo.getEndDate(), 1));
        }
        //查询时，创效月份开始日期
        if (StringUtils.isNotEmpty(monthStartDateStr)) {
            bizcostApplyVo.setMonthStartDate(DateUtils.convertToDate(monthStartDateStr + "-01"));
        }
        //查询时，创效月份结束日期增加一天
        if (StringUtils.isNotEmpty(monthEndDateStr)) {
            bizcostApplyVo.setMonthEndDate(DateUtils.getMonthEnd(DateUtils.convertToDate(
                monthEndDateStr, "yyyy-MM")));
        }
        page = bizcostApplyService.selectPageByDomainWhere(page, bizcostApplyVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    /**
     * 设备合同-我审批的列表数据
     *
     * @return
     */
    @RequestMapping("/queryMyCheckDataByCondition")
    @ResponseBody
    public RspVo<BizcostApplyDomain> queryMyCheckDataByCondition(BizcostApplyVo bizcostApplyVo,
                                                                 ParamPage paramPage,
                                                                 String monthStartDateStr,
                                                                 String monthEndDateStr) {
        Page<BizcostApplyDomain> page = new Page<BizcostApplyDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        //查询时，结束日期增加一天
        if (bizcostApplyVo.getEndDate() != null) {
            bizcostApplyVo.setEndDate(DateUtils.addDays(bizcostApplyVo.getEndDate(), 1));
        }
        //查询时，创效月份开始日期
        if (StringUtils.isNotEmpty(monthStartDateStr)) {
            bizcostApplyVo.setMonthStartDate(DateUtils.convertToDate(monthStartDateStr + "-01"));
        }
        //查询时，创效月份结束日期增加一天
        if (StringUtils.isNotEmpty(monthEndDateStr)) {
            bizcostApplyVo.setMonthEndDate(DateUtils.getMonthEnd(DateUtils.convertToDate(
                monthEndDateStr, "yyyy-MM")));
        }
        bizcostApplyVo.setAddBy(SessionUserUtils.getSessionUserId());
        page = bizcostApplyService.selectMyCheckByDomainWhere(page, bizcostApplyVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    /**
     * 新增
     *
     * @return
     */
    @RequestMapping("/add")
    public String add(HttpServletRequest request) {
        //查询可公司年度预算的公司年度预算记录
        AuthorizationVO authorizationVO = SessionUserUtils.getSessionAttributeForUserDtl();
        StaffInfo staffInfo = staffInfoService.selectByPrimaryKey(authorizationVO.getHrStaffId());
        request.setAttribute("staffName", staffInfo.getStaffName());
        //查询所有项目合同
        List<ProContract> contractList = proContractService.selectAllContracts(null, null);
        request.setAttribute("contractList", contractList);
        return "bpa/lay-bizcostApply-add";
    }

//    /**
//     * 根据费用类型查询对应的合同记录
//     * @param feeType
//     * @return
//     */
//    @RequestMapping("/getCotByFeetype")
//    @ResponseBody
//    public Map<String, Object> getCotByFeetype(String feeType, String proCode) {
//        boolean flag = true;
//        Map<String, Object> map = new HashMap<String, Object>();
//        try {
//            /**
//             * 查询最新的设备合同记录
//             */
//            if (feeType.equals("1")) {//查询内部协议
//                InnerAgrt innerAgrtWhere = new InnerAgrt();
//                innerAgrtWhere.setProcessStatus("9");
//                innerAgrtWhere.setExtType("1");
//                innerAgrtWhere.setProCode(proCode);
//                List<InnerAgrt> innerAgrts = innerAgrtService.selectByEntityWhere(innerAgrtWhere);
//                map.put("cots", innerAgrts);
//            }
//            else if (feeType.equals("2")) {//查询分包合同
//                SubpackCot subpackCotWhere = new SubpackCot();
//                subpackCotWhere.setProcessStatus("9");
//                subpackCotWhere.setExtType("1");
//                subpackCotWhere.setProCode(proCode);
//                List<SubpackCot> subpackCots = subpackCotService.selectByEntityWhere(subpackCotWhere);
//                map.put("cots", subpackCots);
//            }
//            else if (feeType.equals("3")) {//查询设备合同
//                DeviceCot deviceCotWhere = new DeviceCot();
//                deviceCotWhere.setProcessStatus("9");
//                deviceCotWhere.setExtType("1");
//                deviceCotWhere.setProCode(proCode);
//                List<DeviceCot> deviceCots = deviceCotService.selectByEntityWhere(deviceCotWhere);
//                map.put("cots", deviceCots);
//            }
//            else {//查询分包合同
//                OtherCot otherCotWhere = new OtherCot();
//                otherCotWhere.setProcessStatus("9");
//                otherCotWhere.setExtType("1");
//                otherCotWhere.setProCode(proCode);
//                if (feeType.equals("4")) {
//                    otherCotWhere.setCotType(32);
//                }
//                else if (feeType.equals("5")) {
//                    otherCotWhere.setCotType(33);
//                }
//                List<OtherCot> otherCots = otherCotService.selectByEntityWhere(otherCotWhere);
//                map.put("cots", otherCots);
//            }
//        }
//        catch (Exception e) {
//            log.error("查询合同记录异常", e);
//            flag = false;
//        }
//        map.put("flag", flag);
//        return map;
//    }

//    /**
//     * 根据费用类别和支付类别查询对应的合同记录
//     * @param feeType 费用类别
//     * @param payType 支付类别
//     * @param id 合同id
//     * @param sureMonth 确权月份
//     * @param sureMonthEnd 确权结束月份
//     * @return
//     */
//    @RequestMapping("/getCotByFeetypeAndPaytype")
//    @ResponseBody
//    public Map<String, Object> getCotByFeetypeAndPaytype(String feeType, String payType,
//                                                         String id, String sureMonth,
//                                                         String sureMonthEnd) {
//        boolean flag = true;
//        Map<String, Object> map = new HashMap<String, Object>();
//        try {
//            Map<String, Object> mapReturn = new HashMap<String, Object>();
//            Map<String, Object> mapWhere = new HashMap<String, Object>();
//            mapWhere.put("refId", id);
//            mapWhere.put("processStatus", "9");
//            mapWhere.put("orderCondition", "ADD_TIME desc");
//            /**
//             * 查询最新的设备合同记录
//             */
//            if (feeType.equals("1")) {//查询内部协议
//                InnerAgrt innerAgrt = new InnerAgrt();
//                List<InnerAgrt> innerAgrts = innerAgrtService.selectByMapWhere(mapWhere);
//                if (CollectionUtils.isNotEmpty(innerAgrts)) {
//                    innerAgrt = innerAgrtService.selectByPrimaryKey(innerAgrts.get(0).getId());
//                }
//                else {
//                    innerAgrt = innerAgrtService.selectByPrimaryKey(id);
//                }
//                if (payType.equals("1")) {//预付款
//                    mapReturn.put("bizcostApplyDetail.preCotAmount", innerAgrt.getAfterTotalSum());
//                    mapReturn.put("bizcostApplyDetail.preRate", innerAgrt.getPrePayRate());
//                    BigDecimal prePayAmount = innerAgrt.getAfterTotalSum().multiply(
//                        innerAgrt.getPrePayRate()).divide(new BigDecimal(100)).setScale(2,
//                        BigDecimal.ROUND_HALF_UP);;
//                    mapReturn.put("bizcostApplyDetail.prePayAmount", prePayAmount);
//                }
//                else if (payType.equals("2")) {//月度进度款
//                    getPayType12(sureMonth, mapReturn, mapWhere, innerAgrt);
//                }
//                else if (payType.equals("3")) {//年度进度款
//                    getPayType13(sureMonth, sureMonthEnd, mapReturn, mapWhere, innerAgrt);
//                }
//                else if (payType.equals("4")) {//竣工付费
//                    getPayType14(sureMonth, mapReturn, mapWhere, innerAgrt);
//                }
//                else if (payType.equals("5")) {//结算工程款
//                    getPayType15(mapReturn, mapWhere, innerAgrt);
//                }
//                else if (payType.equals("6")) {//质保金返还
//                    getPayType16(mapReturn, mapWhere, innerAgrt);
//                }
//            }
//            else if (feeType.equals("2")) {//查询分包合同
//                SubpackCot subpackCot = new SubpackCot();
//                List<SubpackCot> subpackCots = subpackCotService.selectByMapWhere(mapWhere);
//                if (CollectionUtils.isNotEmpty(subpackCots)) {
//                    subpackCot = subpackCotService.selectByPrimaryKey(subpackCots.get(0).getId());
//                }
//                else {
//                    subpackCot = subpackCotService.selectByPrimaryKey(id);
//                }
//                if (payType.equals("1")) {//预付款
//                    BigDecimal preCotAmount = BigDecimal.ZERO;
//                    if (subpackCot.getCotType().equals("1")) {//整体下浮
//                        preCotAmount = subpackCot.getAfterTotalSum();
//                    }
//                    else {
//                        preCotAmount = subpackCot.getTotalSumWhen();
//                    }
//                    mapReturn.put("bizcostApplyDetail.preCotAmount", preCotAmount);
//                    mapReturn.put("bizcostApplyDetail.preRate", subpackCot.getPrePayRate());
//                    BigDecimal prePayAmount = preCotAmount.multiply(subpackCot.getPrePayRate()).divide(
//                        new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP);;
//                    mapReturn.put("bizcostApplyDetail.prePayAmount", prePayAmount);
//                }
//                else if (payType.equals("2")) {//月度进度款
//                    getPayType22(sureMonth, mapReturn, mapWhere, subpackCot);
//                }
//                else if (payType.equals("3")) {//年度进度款
//                    getPayType23(sureMonth, sureMonthEnd, mapReturn, mapWhere, subpackCot);
//                }
//                else if (payType.equals("4")) {//竣工付费
//                    getPayType24(sureMonth, mapReturn, mapWhere, subpackCot);
//                }
//                else if (payType.equals("5")) {//结算工程款
//                    getPayType25(mapReturn, mapWhere, subpackCot);
//                }
//                else if (payType.equals("6")) {//质保金返还
//                    getPayType26(mapReturn, mapWhere, subpackCot);
//                }
//            }
//            else if (feeType.equals("3")) {//查询设备合同
//                /**
//                 * 查询最新的设备合同记录
//                 */
//                DeviceCot deviceCot = new DeviceCot();
//                List<DeviceCot> deviceCots = deviceCotService.selectByMapWhere(mapWhere);
//                if (CollectionUtils.isNotEmpty(deviceCots)) {
//                    deviceCot = deviceCotService.selectByPrimaryKey(deviceCots.get(0).getId());
//                }
//                else {
//                    deviceCot = deviceCotService.selectByPrimaryKey(id);
//                }
//                if (payType.equals("1")) {//预付款
//                    mapReturn.put("bizcostApplyDetail.preCotAmount", deviceCot.getSumTotal());
//                    mapReturn.put("bizcostApplyDetail.preRate", deviceCot.getPrePayRate());
//                    BigDecimal prePayAmount = deviceCot.getSumTotal().multiply(
//                        deviceCot.getPrePayRate()).divide(new BigDecimal(100)).setScale(2,
//                        BigDecimal.ROUND_HALF_UP);;
//                    mapReturn.put("bizcostApplyDetail.prePayAmount", prePayAmount);
//                }
//                else if (payType.equals("2")) {//月度进度款
//                    getPayType32(sureMonth, mapReturn, mapWhere, deviceCot);
//                }
//                else if (payType.equals("3")) {//年度进度款
//                    getPayType33(sureMonth, sureMonthEnd, mapReturn, mapWhere, deviceCot);
//                }
//                else if (payType.equals("4")) {//竣工付费
//                    getPayType34(sureMonth, mapReturn, mapWhere, deviceCot);
//                }
//                else if (payType.equals("5")) {//结算工程款
//                    getPayType35(mapReturn, mapWhere, deviceCot);
//                }
//                else if (payType.equals("6")) {//质保金返还
//                    getPayType36(mapReturn, mapWhere, deviceCot);
//                }
//            }
//            else {//查询其他费合同确权
//                OtherSure otherSure = new OtherSure();
//                if (StringUtils.isNotBlank(sureMonth)) {
//                    Date stsMonth = DateUtils.parseDate(sureMonth, "yyyy-MM");
//                    mapWhere.put("stsMonthStart", stsMonth);
//                    Date stsMonthEnd = DateUtils.getMonthEnd(stsMonth);
//                    mapWhere.put("stsMonthEnd", stsMonthEnd);
//                    List<OtherSure> otherSures = otherSureService.selectByMapWhere(mapWhere);
//                    if (CollectionUtils.isNotEmpty(otherSures)) {
//                        otherSure = otherSures.get(0);
//                    }
//                }
//                if (payType.equals("7")) {//代付费
//                    mapReturn.put(
//                        "bizcostApplyDetail.replaceSureAmout",
//                        otherSure.getMonthSureAmount() == null ? BigDecimal.ZERO : otherSure.getMonthSureAmount());
//                }
//                else if (payType.equals("8")) {//前期费
//                    mapReturn.put(
//                        "bizcostApplyDetail.frontSureAmount",
//                        otherSure.getMonthSureAmount() == null ? BigDecimal.ZERO : otherSure.getMonthSureAmount());
//                }
//            }
//            map.put("cotVo", mapReturn);
//        }
//        catch (Exception e) {
//            log.error("查询合同记录异常", e);
//            flag = false;
//        }
//        map.put("flag", flag);
//        return map;
//    }

//    private void getPayType36(Map<String, Object> mapReturn, Map<String, Object> mapWhere,
//                              DeviceCot deviceCot) {
//        BigDecimal backProAmount = BigDecimal.ZERO;//工程结算价（元）
//        BigDecimal backQaRate = BigDecimal.ZERO;//质保金比例（%）
//        BigDecimal backQaAct = BigDecimal.ZERO;//实扣质保金（元）
//        mapWhere.put("extType", "3");
//        List<InnerSettle> innerSettles = innerSettleService.selectByMapWhere(mapWhere);
//        if (CollectionUtils.isNotEmpty(innerSettles)) {
//            for (InnerSettle innerSettle : innerSettles) {
//                backProAmount = backProAmount.add(innerSettle.getTotalSumInner());
//            }
//        }
//        if (deviceCot.getQaEnsureRate() != null) {
//            backQaRate = deviceCot.getQaEnsureRate();
//        }
//        backQaAct = backProAmount.multiply(backQaRate).divide(new BigDecimal(100)).setScale(2,
//            BigDecimal.ROUND_HALF_UP);
//        mapReturn.put("bizcostApplyDetail.backProAmount", backProAmount);
//        mapReturn.put("bizcostApplyDetail.backQaRate", backQaRate);
//        mapReturn.put("bizcostApplyDetail.backQaAct", backQaAct);
//    }

//    private void getPayType26(Map<String, Object> mapReturn, Map<String, Object> mapWhere,
//                              SubpackCot subpackCot) {
//        BigDecimal backProAmount = BigDecimal.ZERO;//工程结算价（元）
//        BigDecimal backQaRate = BigDecimal.ZERO;//质保金比例（%）
//        BigDecimal backQaAct = BigDecimal.ZERO;//实扣质保金（元）
//        mapWhere.put("extType", "2");
//        List<InnerSettle> innerSettles = innerSettleService.selectByMapWhere(mapWhere);
//        if (CollectionUtils.isNotEmpty(innerSettles)) {
//            for (InnerSettle innerSettle : innerSettles) {
//                backProAmount = backProAmount.add(innerSettle.getTotalSumInner());
//            }
//        }
//        if (subpackCot.getQaEnsureRate() != null) {
//            backQaRate = subpackCot.getQaEnsureRate();
//        }
//        backQaAct = backProAmount.multiply(backQaRate).divide(new BigDecimal(100)).setScale(2,
//            BigDecimal.ROUND_HALF_UP);
//        mapReturn.put("bizcostApplyDetail.backProAmount", backProAmount);
//        mapReturn.put("bizcostApplyDetail.backQaRate", backQaRate);
//        mapReturn.put("bizcostApplyDetail.backQaAct", backQaAct);
//    }

//    private void getPayType16(Map<String, Object> mapReturn, Map<String, Object> mapWhere,
//                              InnerAgrt innerAgrt) {
//        BigDecimal backProAmount = BigDecimal.ZERO;//工程结算价（元）
//        BigDecimal backQaRate = BigDecimal.ZERO;//质保金比例（%）
//        BigDecimal backQaAct = BigDecimal.ZERO;//实扣质保金（元）
//        mapWhere.put("extType", "1");
//        List<InnerSettle> innerSettles = innerSettleService.selectByMapWhere(mapWhere);
//        if (CollectionUtils.isNotEmpty(innerSettles)) {
//            for (InnerSettle innerSettle : innerSettles) {
//                backProAmount = backProAmount.add(innerSettle.getTotalSumInner());
//            }
//        }
//        if (innerAgrt.getQaEnsureRate() != null) {
//            backQaRate = innerAgrt.getQaEnsureRate();
//        }
//        backQaAct = backProAmount.multiply(backQaRate).divide(new BigDecimal(100)).setScale(2,
//            BigDecimal.ROUND_HALF_UP);
//        mapReturn.put("bizcostApplyDetail.backProAmount", backProAmount);
//        mapReturn.put("bizcostApplyDetail.backQaRate", backQaRate);
//        mapReturn.put("bizcostApplyDetail.backQaAct", backQaAct);
//    }
//
//    private void getPayType35(Map<String, Object> mapReturn, Map<String, Object> mapWhere,
//                              DeviceCot deviceCot) {
//        BigDecimal settProAmount = BigDecimal.ZERO;//工程结算价（元）
//        BigDecimal settPayRate = BigDecimal.ZERO;//结算付款比例（%）
//        BigDecimal settShouldFee = BigDecimal.ZERO;//应付款（元）
//        mapWhere.put("extType", "3");
//        List<InnerSettle> innerSettles = innerSettleService.selectByMapWhere(mapWhere);
//        if (CollectionUtils.isNotEmpty(innerSettles)) {
//            for (InnerSettle innerSettle : innerSettles) {
//                settProAmount = settProAmount.add(innerSettle.getTotalSumInner());
//            }
//        }
//        if (deviceCot.getSettleAfterRate() != null) {
//            settPayRate = deviceCot.getSettleAfterRate();
//        }
//        settShouldFee = settProAmount.multiply(settPayRate).divide(new BigDecimal(100)).setScale(
//            2, BigDecimal.ROUND_HALF_UP);
//        mapReturn.put("bizcostApplyDetail.settProAmount", settProAmount);
//        mapReturn.put("bizcostApplyDetail.settPayRate", settPayRate);
//        mapReturn.put("bizcostApplyDetail.settShouldFee", settShouldFee);
//    }

//    private void getPayType25(Map<String, Object> mapReturn, Map<String, Object> mapWhere,
//                              SubpackCot subpackCot) {
//        BigDecimal settProAmount = BigDecimal.ZERO;//工程结算价（元）
//        BigDecimal settPayRate = BigDecimal.ZERO;//结算付款比例（%）
//        BigDecimal settShouldFee = BigDecimal.ZERO;//应付款（元）
//        mapWhere.put("extType", "2");
//        List<InnerSettle> innerSettles = innerSettleService.selectByMapWhere(mapWhere);
//        if (CollectionUtils.isNotEmpty(innerSettles)) {
//            for (InnerSettle innerSettle : innerSettles) {
//                settProAmount = settProAmount.add(innerSettle.getTotalSumInner());
//            }
//        }
//        if (subpackCot.getSettleAfterRate() != null) {
//            settPayRate = subpackCot.getSettleAfterRate();
//        }
//        settShouldFee = settProAmount.multiply(settPayRate).divide(new BigDecimal(100)).setScale(
//            2, BigDecimal.ROUND_HALF_UP);
//        mapReturn.put("bizcostApplyDetail.settProAmount", settProAmount);
//        mapReturn.put("bizcostApplyDetail.settPayRate", settPayRate);
//        mapReturn.put("bizcostApplyDetail.settShouldFee", settShouldFee);
//    }

//    private void getPayType15(Map<String, Object> mapReturn, Map<String, Object> mapWhere,
//                              InnerAgrt innerAgrt) {
//        BigDecimal settProAmount = BigDecimal.ZERO;//工程结算价（元）
//        BigDecimal settPayRate = BigDecimal.ZERO;//结算付款比例（%）
//        BigDecimal settShouldFee = BigDecimal.ZERO;//应付款（元）
//        mapWhere.put("extType", "1");
//        List<InnerSettle> innerSettles = innerSettleService.selectByMapWhere(mapWhere);
//        if (CollectionUtils.isNotEmpty(innerSettles)) {
//            for (InnerSettle innerSettle : innerSettles) {
//                settProAmount = settProAmount.add(innerSettle.getTotalSumInner());
//            }
//        }
//        if (innerAgrt.getSettleAfterRate() != null) {
//            settPayRate = innerAgrt.getSettleAfterRate();
//        }
//        settShouldFee = settProAmount.multiply(settPayRate).divide(new BigDecimal(100)).setScale(
//            2, BigDecimal.ROUND_HALF_UP);
//        mapReturn.put("bizcostApplyDetail.settProAmount", settProAmount);
//        mapReturn.put("bizcostApplyDetail.settPayRate", settPayRate);
//        mapReturn.put("bizcostApplyDetail.settShouldFee", settShouldFee);
//    }

//    private void getPayType34(String sureMonth, Map<String, Object> mapReturn,
//                              Map<String, Object> mapWhere, DeviceCot deviceCot)
//        throws ParseException {
//        BigDecimal overAddupAmount = BigDecimal.ZERO;//开累确权产值（元）
//        BigDecimal overPayRate = BigDecimal.ZERO;//竣工付款比例（%）
//        BigDecimal overShouldAmount = BigDecimal.ZERO;//竣工应付金额（元）
//        Date stsMonth = DateUtils.parseDate(sureMonth, "yyyy-MM");
//        Date stsMonthStart = DateUtils.parseDate("1988-01", "yyyy-MM");
//        mapWhere.put("stsMonthStart", stsMonthStart);
//        Date stsMonthEnd = DateUtils.getMonthEnd(stsMonth);
//        mapWhere.put("stsMonthEnd", stsMonthEnd);
//        List<DeviceSure> deviceSures = deviceSureService.selectByMapWhere(mapWhere);
//        if (CollectionUtils.isNotEmpty(deviceSures)) {
//            for (DeviceSure deviceSure : deviceSures) {
//                overAddupAmount = overAddupAmount.add(deviceSure.getMonthSureAmount());
//            }
//        }
//        if (deviceCot.getCheckAfterRate() != null) {
//            overPayRate = deviceCot.getCheckAfterRate();
//        }
//        overShouldAmount = overAddupAmount.multiply(overPayRate).divide(new BigDecimal(100)).setScale(
//            2, BigDecimal.ROUND_HALF_UP);
//        mapReturn.put("bizcostApplyDetail.overAddupAmount", overAddupAmount);
//        mapReturn.put("bizcostApplyDetail.overPayRate", overPayRate);
//        mapReturn.put("bizcostApplyDetail.overShouldAmount", overShouldAmount);
//    }

//    private void getPayType24(String sureMonth, Map<String, Object> mapReturn,
//                              Map<String, Object> mapWhere, SubpackCot subpackCot)
//        throws ParseException {
//        BigDecimal overAddupAmount = BigDecimal.ZERO;//开累确权产值（元）
//        BigDecimal overPayRate = BigDecimal.ZERO;//竣工付款比例（%）
//        BigDecimal overShouldAmount = BigDecimal.ZERO;//竣工应付金额（元）
//        mapWhere.put("cotType", "2");
//        Date stsMonth = DateUtils.parseDate(sureMonth, "yyyy-MM");
//        Date stsMonthStart = DateUtils.parseDate("1988-01", "yyyy-MM");
//        mapWhere.put("stsMonthStart", stsMonthStart);
//        Date stsMonthEnd = DateUtils.getMonthEnd(stsMonth);
//        mapWhere.put("stsMonthEnd", stsMonthEnd);
//        List<IncaeSure> incaeSures = incaeSureService.selectByMapWhere(mapWhere);
//        if (CollectionUtils.isNotEmpty(incaeSures)) {
//            for (IncaeSure incaeSure : incaeSures) {
//                overAddupAmount = overAddupAmount.add(incaeSure.getMonthAfterAmount());
//            }
//        }
//        if (subpackCot.getCheckAfterRate() != null) {
//            overPayRate = subpackCot.getCheckAfterRate();
//        }
//        overShouldAmount = overAddupAmount.multiply(overPayRate).divide(new BigDecimal(100)).setScale(
//            2, BigDecimal.ROUND_HALF_UP);
//        mapReturn.put("bizcostApplyDetail.overAddupAmount", overAddupAmount);
//        mapReturn.put("bizcostApplyDetail.overPayRate", overPayRate);
//        mapReturn.put("bizcostApplyDetail.overShouldAmount", overShouldAmount);
//    }

//    private void getPayType14(String sureMonth, Map<String, Object> mapReturn,
//                              Map<String, Object> mapWhere, InnerAgrt innerAgrt)
//        throws ParseException {
//        BigDecimal overAddupAmount = BigDecimal.ZERO;//开累确权产值（元）
//        BigDecimal overPayRate = BigDecimal.ZERO;//竣工付款比例（%）
//        BigDecimal overShouldAmount = BigDecimal.ZERO;//竣工应付金额（元）
//        mapWhere.put("cotType", "1");
//        Date stsMonth = DateUtils.parseDate(sureMonth, "yyyy-MM");
//        Date stsMonthStart = DateUtils.parseDate("1988-01", "yyyy-MM");
//        mapWhere.put("stsMonthStart", stsMonthStart);
//        Date stsMonthEnd = DateUtils.getMonthEnd(stsMonth);
//        mapWhere.put("stsMonthEnd", stsMonthEnd);
//        List<IncaeSure> incaeSures = incaeSureService.selectByMapWhere(mapWhere);
//        if (CollectionUtils.isNotEmpty(incaeSures)) {
//            for (IncaeSure incaeSure : incaeSures) {
//                overAddupAmount = overAddupAmount.add(incaeSure.getMonthAfterAmount());
//            }
//        }
//        if (innerAgrt.getCheckAfterRate() != null) {
//            overPayRate = innerAgrt.getCheckAfterRate();
//        }
//        overShouldAmount = overAddupAmount.multiply(overPayRate).divide(new BigDecimal(100)).setScale(
//            2, BigDecimal.ROUND_HALF_UP);
//        mapReturn.put("bizcostApplyDetail.overAddupAmount", overAddupAmount);
//        mapReturn.put("bizcostApplyDetail.overPayRate", overPayRate);
//        mapReturn.put("bizcostApplyDetail.overShouldAmount", overShouldAmount);
//    }

//    private void getPayType33(String sureMonth, String sureMonthEnd,
//                              Map<String, Object> mapReturn, Map<String, Object> mapWhere,
//                              DeviceCot deviceCot)
//        throws ParseException {
//        BigDecimal yearSureAmount = BigDecimal.ZERO;//年度确权产值（元）
//        BigDecimal yearPayRate = BigDecimal.ZERO;//年度付款比例（%）
//        BigDecimal yearShouldAmount = BigDecimal.ZERO;//年度应付金额（元）
//        Date stsMonth = DateUtils.parseDate(sureMonth, "yyyy-MM");
//        Date sMonthEnd = DateUtils.parseDate(sureMonthEnd, "yyyy-MM");
//        mapWhere.put("stsMonthStart", stsMonth);
//        Date stsMonthEnd = DateUtils.getMonthEnd(sMonthEnd);
//        mapWhere.put("stsMonthEnd", stsMonthEnd);
//        List<DeviceSure> deviceSures = deviceSureService.selectByMapWhere(mapWhere);
//        if (CollectionUtils.isNotEmpty(deviceSures)) {
//            for (DeviceSure deviceSure : deviceSures) {
//                yearSureAmount = yearSureAmount.add(deviceSure.getMonthSureAmount());
//            }
//        }
//        if (deviceCot.getYearPayRate() != null) {
//            yearPayRate = deviceCot.getYearPayRate();
//        }
//        yearShouldAmount = yearSureAmount.multiply(yearPayRate).divide(new BigDecimal(100)).setScale(
//            2, BigDecimal.ROUND_HALF_UP);
//        mapReturn.put("bizcostApplyDetail.yearSureAmount", yearSureAmount);
//        mapReturn.put("bizcostApplyDetail.yearPayRate", yearPayRate);
//        mapReturn.put("bizcostApplyDetail.yearShouldAmount", yearShouldAmount);
//    }

//    private void getPayType23(String sureMonth, String sureMonthEnd,
//                              Map<String, Object> mapReturn, Map<String, Object> mapWhere,
//                              SubpackCot subpackCot)
//        throws ParseException {
//        BigDecimal yearSureAmount = BigDecimal.ZERO;//年度确权产值（元）
//        BigDecimal yearPayRate = BigDecimal.ZERO;//年度付款比例（%）
//        BigDecimal yearShouldAmount = BigDecimal.ZERO;//年度应付金额（元）
//        mapWhere.put("cotType", "2");
//        Date stsMonth = DateUtils.parseDate(sureMonth, "yyyy-MM");
//        Date sMonthEnd = DateUtils.parseDate(sureMonthEnd, "yyyy-MM");
//        mapWhere.put("stsMonthStart", stsMonth);
//        Date stsMonthEnd = DateUtils.getMonthEnd(sMonthEnd);
//        mapWhere.put("stsMonthEnd", stsMonthEnd);
//        List<IncaeSure> incaeSures = incaeSureService.selectByMapWhere(mapWhere);
//        if (CollectionUtils.isNotEmpty(incaeSures)) {
//            for (IncaeSure incaeSure : incaeSures) {
//                yearSureAmount = yearSureAmount.add(incaeSure.getMonthAfterAmount());
//            }
//        }
//        if (subpackCot.getYearPayRate() != null) {
//            yearPayRate = subpackCot.getYearPayRate();
//        }
//        yearShouldAmount = yearSureAmount.multiply(yearPayRate).divide(new BigDecimal(100)).setScale(
//            2, BigDecimal.ROUND_HALF_UP);
//        mapReturn.put("bizcostApplyDetail.yearSureAmount", yearSureAmount);
//        mapReturn.put("bizcostApplyDetail.yearPayRate", yearPayRate);
//        mapReturn.put("bizcostApplyDetail.yearShouldAmount", yearShouldAmount);
//    }

//    private void getPayType13(String sureMonth, String sureMonthEnd,
//                              Map<String, Object> mapReturn, Map<String, Object> mapWhere,
//                              InnerAgrt innerAgrt)
//        throws ParseException {
//        BigDecimal yearSureAmount = BigDecimal.ZERO;//年度确权产值（元）
//        BigDecimal yearPayRate = BigDecimal.ZERO;//年度付款比例（%）
//        BigDecimal yearShouldAmount = BigDecimal.ZERO;//年度应付金额（元）
//        mapWhere.put("cotType", "1");
//        Date stsMonth = DateUtils.parseDate(sureMonth, "yyyy-MM");
//        Date sMonthEnd = DateUtils.parseDate(sureMonthEnd, "yyyy-MM");
//        mapWhere.put("stsMonthStart", stsMonth);
//        Date stsMonthEnd = DateUtils.getMonthEnd(sMonthEnd);
//        mapWhere.put("stsMonthEnd", stsMonthEnd);
//        List<IncaeSure> incaeSures = incaeSureService.selectByMapWhere(mapWhere);
//        if (CollectionUtils.isNotEmpty(incaeSures)) {
//            for (IncaeSure incaeSure : incaeSures) {
//                yearSureAmount = yearSureAmount.add(incaeSure.getMonthAfterAmount());
//            }
//        }
//        if (innerAgrt.getYearPayRate() != null) {
//            yearPayRate = innerAgrt.getYearPayRate();
//        }
//        yearShouldAmount = yearSureAmount.multiply(yearPayRate).divide(new BigDecimal(100)).setScale(
//            2, BigDecimal.ROUND_HALF_UP);
//        mapReturn.put("bizcostApplyDetail.yearSureAmount", yearSureAmount);
//        mapReturn.put("bizcostApplyDetail.yearPayRate", yearPayRate);
//        mapReturn.put("bizcostApplyDetail.yearShouldAmount", yearShouldAmount);
//    }

//    private void getPayType32(String sureMonth, Map<String, Object> mapReturn,
//                              Map<String, Object> mapWhere, DeviceCot deviceCot)
//        throws ParseException {
//        BigDecimal monthSureAmount = BigDecimal.ZERO;//已确权产值（元）
//        BigDecimal monthPayRate = BigDecimal.ZERO;//月度付款比例（%）
//        DeviceSure deviceSure = new DeviceSure();
//        Date stsMonth = DateUtils.parseDate(sureMonth, "yyyy-MM");
//        mapWhere.put("stsMonthStart", stsMonth);
//        Date stsMonthEnd = DateUtils.getMonthEnd(stsMonth);
//        mapWhere.put("stsMonthEnd", stsMonthEnd);
//        List<DeviceSure> deviceSures = deviceSureService.selectByMapWhere(mapWhere);
//        if (CollectionUtils.isNotEmpty(deviceSures)) {
//            deviceSure = deviceSures.get(0);
//        }
//        if (deviceSure.getMonthSureAmount() != null) {
//            monthSureAmount = deviceSure.getMonthSureAmount();
//        }
//        if (deviceCot.getMonthPayRate() != null) {
//            monthPayRate = deviceCot.getMonthPayRate();
//        }
//        mapReturn.put("bizcostApplyDetail.monthSureAmount", monthSureAmount);
//        mapReturn.put("bizcostApplyDetail.monthPayRate", monthPayRate);
//        BigDecimal monthAdjustRate = new BigDecimal(100);//综合计划调整付款比例（%）
//        mapReturn.put("bizcostApplyDetail.monthAdjustRate", monthAdjustRate);
//        //当月实际付款比例（%）
//        BigDecimal monthActRate = monthPayRate.multiply(monthAdjustRate).divide(
//            new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP);
//        mapReturn.put("bizcostApplyDetail.monthActRate", monthActRate);
//        //当月应付金额（元）
//        BigDecimal monthShouldAmount = monthSureAmount.multiply(monthActRate).divide(
//            new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP);//当月实际付款比例（%）
//        mapReturn.put("bizcostApplyDetail.monthShouldAmount", monthShouldAmount);
//    }

//    private void getPayType22(String sureMonth, Map<String, Object> mapReturn,
//                              Map<String, Object> mapWhere, SubpackCot subpackCot)
//        throws ParseException {
//        BigDecimal monthSureAmount = BigDecimal.ZERO;//已确权产值（元）
//        BigDecimal monthPayRate = BigDecimal.ZERO;//月度付款比例（%）
//        IncaeSure incaeSure = new IncaeSure();
//        mapWhere.put("cotType", "2");
//        Date stsMonth = DateUtils.parseDate(sureMonth, "yyyy-MM");
//        mapWhere.put("stsMonthStart", stsMonth);
//        Date stsMonthEnd = DateUtils.getMonthEnd(stsMonth);
//        mapWhere.put("stsMonthEnd", stsMonthEnd);
//        List<IncaeSure> incaeSures = incaeSureService.selectByMapWhere(mapWhere);
//        if (CollectionUtils.isNotEmpty(incaeSures)) {
//            incaeSure = incaeSures.get(0);
//        }
//        if (incaeSure.getMonthAfterAmount() != null) {
//            monthSureAmount = incaeSure.getMonthAfterAmount();
//        }
//        if (subpackCot.getMonthPayRate() != null) {
//            monthPayRate = subpackCot.getMonthPayRate();
//        }
//        mapReturn.put("bizcostApplyDetail.monthSureAmount", monthSureAmount);
//        mapReturn.put("bizcostApplyDetail.monthPayRate", monthPayRate);
//        //查询月度管控记录
//        MonthCtrl monthCtrl = new MonthCtrl();
//        BigDecimal packPayRate = BigDecimal.ZERO;//总包付款完成率
//        Map<String, Object> mapMonthCtrlWhere = new HashMap<String, Object>();
//        if (StringUtils.isNotBlank(sureMonth)) {
//            mapMonthCtrlWhere.put("stsMonthStart", stsMonth);
//            mapMonthCtrlWhere.put("stsMonthEnd", stsMonthEnd);
//            mapMonthCtrlWhere.put("proCode", subpackCot.getProCode());
//            mapMonthCtrlWhere.put("processStatus", "9");
//            mapMonthCtrlWhere.put("orderCondition", "ADD_TIME desc");
//            List<MonthCtrl> monthCtrls = monthCtrlService.selectByMapWhere(mapMonthCtrlWhere);
//            if (CollectionUtils.isNotEmpty(monthCtrls)) {
//                monthCtrl = monthCtrls.get(0);
//            }
//            if (StringUtils.isNotEmpty(monthCtrl.getId())) {
//                //查询分包付款完成率清单
//                MonthCtrlSubpack monthCtrlSubpack = new MonthCtrlSubpack();
//                monthCtrlSubpack.setMonthCtrlId(monthCtrl.getId());
//                monthCtrlSubpack.setCotName(subpackCot.getSubpackCotName());
//                List<MonthCtrlSubpack> monthCtrlSubpacks = monthCtrlSubpackService.selectByEntityWhere(monthCtrlSubpack);
//                if (CollectionUtils.isNotEmpty(monthCtrlSubpacks)) {
//                    packPayRate = monthCtrlSubpacks.get(0).getSubpackPayRate();
//                }
//            }
//        }
//        BigDecimal monthAdjustRate = BigDecimal.ZERO;//综合计划调整付款比例（%）
//        String applyDateStr = BizParaUtil.get("bpa_bizcost_apply_date");//分水岭日期
//        Date applyDate = DateUtils.parseDate(applyDateStr, "yyyy-MM-dd");
//        //小于分水岭日期时，返回100
//        if (DateUtils.compareDate(stsMonth, applyDate) < 0) {
//            monthAdjustRate = new BigDecimal(100);
//        }
//        else {
//            Map<String, Object> mapRateWhere = new HashMap<String, Object>();
//            mapRateWhere.put("subpackCotId", subpackCot.getId());
//            List<SubpackCotRate> subpackCotRates = subpackCotRateService.selectByMapWhere(mapRateWhere);
//            if (CollectionUtils.isNotEmpty(subpackCotRates)) {
//                for (SubpackCotRate subpackCotRate : subpackCotRates) {
//                    if (packPayRate.compareTo(subpackCotRate.getMinValue()) >= 0
//                        && packPayRate.compareTo(subpackCotRate.getMaxValue()) < 0) {
//                        monthAdjustRate = subpackCotRate.getPayRate();
//                        break;
//                    }
//                }
//            }
//        }
//        mapReturn.put("bizcostApplyDetail.monthAdjustRate", monthAdjustRate);
//        //当月实际付款比例（%）
//        BigDecimal monthActRate = monthPayRate.multiply(monthAdjustRate).divide(
//            new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP);
//        mapReturn.put("bizcostApplyDetail.monthActRate", monthActRate);
//        //当月应付金额（元）
//        BigDecimal monthShouldAmount = monthSureAmount.multiply(monthActRate).divide(
//            new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP);//当月实际付款比例（%）
//        mapReturn.put("bizcostApplyDetail.monthShouldAmount", monthShouldAmount);
//    }

//    private void getPayType12(String sureMonth, Map<String, Object> mapReturn,
//                              Map<String, Object> mapWhere, InnerAgrt innerAgrt)
//        throws ParseException {
//        BigDecimal monthSureAmount = BigDecimal.ZERO;//已确权产值（元）
//        BigDecimal monthPayRate = BigDecimal.ZERO;//月度付款比例（%）
//        IncaeSure incaeSure = new IncaeSure();
//        mapWhere.put("cotType", "1");
//        Date stsMonth = DateUtils.parseDate(sureMonth, "yyyy-MM");
//        mapWhere.put("stsMonthStart", stsMonth);
//        Date stsMonthEnd = DateUtils.getMonthEnd(stsMonth);
//        mapWhere.put("stsMonthEnd", stsMonthEnd);
//        List<IncaeSure> incaeSures = incaeSureService.selectByMapWhere(mapWhere);
//        if (CollectionUtils.isNotEmpty(incaeSures)) {
//            incaeSure = incaeSures.get(0);
//        }
//        if (incaeSure.getMonthAfterAmount() != null) {
//            monthSureAmount = incaeSure.getMonthAfterAmount();
//        }
//        if (innerAgrt.getMonthPayRate() != null) {
//            monthPayRate = innerAgrt.getMonthPayRate();
//        }
//        mapReturn.put("bizcostApplyDetail.monthSureAmount", monthSureAmount);
//        mapReturn.put("bizcostApplyDetail.monthPayRate", monthPayRate);
//        //查询月度管控记录
//        MonthCtrl monthCtrl = new MonthCtrl();
//        BigDecimal packPayRate = BigDecimal.ZERO;//总包付款完成率
//        Map<String, Object> mapMonthCtrlWhere = new HashMap<String, Object>();
//        if (StringUtils.isNotBlank(sureMonth)) {
//            mapMonthCtrlWhere.put("stsMonthStart", stsMonth);
//            mapMonthCtrlWhere.put("stsMonthEnd", stsMonthEnd);
//            mapMonthCtrlWhere.put("proCode", innerAgrt.getProCode());
//            mapMonthCtrlWhere.put("processStatus", "9");
//            mapMonthCtrlWhere.put("orderCondition", "ADD_TIME desc");
//            List<MonthCtrl> monthCtrls = monthCtrlService.selectByMapWhere(mapMonthCtrlWhere);
//            if (CollectionUtils.isNotEmpty(monthCtrls)) {
//                monthCtrl = monthCtrls.get(0);
//            }
//            if (monthCtrl.getPackPayRate() != null) {
//                packPayRate = monthCtrl.getPackPayRate();
//            }
//        }
//        BigDecimal monthAdjustRate = BigDecimal.ZERO;//综合计划调整付款比例（%）
//        String applyDateStr = BizParaUtil.get("bpa_bizcost_apply_date");//分水岭日期
//        Date applyDate = DateUtils.parseDate(applyDateStr, "yyyy-MM-dd");
//        //小于分水岭日期时，返回100
//        if (DateUtils.compareDate(stsMonth, applyDate) < 0) {
//            monthAdjustRate = new BigDecimal(100);
//        }
//        else {
//            Map<String, Object> mapRateWhere = new HashMap<String, Object>();
//            mapRateWhere.put("innerAgrtId", innerAgrt.getId());
//            List<InnerAgrtRate> innerAgrtRates = innerAgrtRateService.selectByMapWhere(mapRateWhere);
//            if (CollectionUtils.isNotEmpty(innerAgrtRates)) {
//                for (InnerAgrtRate innerAgrtRate : innerAgrtRates) {
//                    if (packPayRate.compareTo(innerAgrtRate.getMinValue()) >= 0
//                        && packPayRate.compareTo(innerAgrtRate.getMaxValue()) < 0) {
//                        monthAdjustRate = innerAgrtRate.getPayRate();
//                        break;
//                    }
//                }
//            }
//        }
//        mapReturn.put("bizcostApplyDetail.monthAdjustRate", monthAdjustRate);
//        //当月实际付款比例（%）
//        BigDecimal monthActRate = monthPayRate.multiply(monthAdjustRate).divide(
//            new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP);
//        mapReturn.put("bizcostApplyDetail.monthActRate", monthActRate);
//        //当月应付金额（元）
//        BigDecimal monthShouldAmount = monthSureAmount.multiply(monthActRate).divide(
//            new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP);//当月实际付款比例（%）
//        mapReturn.put("bizcostApplyDetail.monthShouldAmount", monthShouldAmount);
//    }

    /**
     * 新增
     * @throws ParseException 
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping("/save")
    @ResponseBody
    public RspVo save(HttpServletRequest request, BizcostApplyVo bizcostApply,
                      String sureMonthStr, String yearPayBeginStr, String yearPayEndStr)
        throws ParseException {
        String[] fileAtt1 = request.getParameterValues("fileAtt1");
        String[] fileAtt2 = request.getParameterValues("fileAtt2");
        String[] fileAtt3 = request.getParameterValues("fileAtt3");

        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        String id = IdFactory.getUUIDUpper();
        bizcostApply.setId(id);
        bizcostApply.setHrStaffApplyId(vo.getHrStaffId());
        bizcostApply.setHrOrgId(String.valueOf(SessionUserUtils.getSessionAttribute("bindOrgId")));
        bizcostApply.setHrDeptId(String.valueOf(SessionUserUtils.getSessionAttribute("bindDeptId")));
        StaffInfo staffInfo = staffInfoService.selectByPrimaryKey(vo.getHrStaffId());
        bizcostApply.setStaffName(staffInfo.getStaffName());
        bizcostApply.setHrStaffApplyId(vo.getHrStaffId());
        bizcostApply.setApplyPayDate(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
        bizcostApply.setAddBy(vo.getId());
        bizcostApply.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
        String codes = codeGeneratorConfigService.generatorCode("bpa_bizcost_apply");
        bizcostApply.setCodes(codes);
        bizcostApply.setProcessId(IdFactory.getUUIDUpper());
        Date month = DateUtils.parseDate(sureMonthStr, "yyyy-MM");
        bizcostApply.setSureMonth(month);
        //设置审批状态
        bizcostApply.setProcessStatus("1");
        bizcostApply.setUptBy(vo.getId());
        bizcostApply.setUptTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());

        //子表数据处理
        BizcostApplyDetail bizcostApplyDetail = bizcostApply.getBizcostApplyDetail();
        String detailId = IdFactory.getUUIDUpper();
        bizcostApplyDetail.setId(detailId);
        bizcostApplyDetail.setApplyId(id);
        if (StringUtils.isNotEmpty(yearPayBeginStr) && StringUtils.isNotEmpty(yearPayEndStr)) {
            Date beginMonth = DateUtils.parseDate(yearPayBeginStr, "yyyy-MM");
            Date endMonth = DateUtils.parseDate(yearPayEndStr, "yyyy-MM");
            bizcostApplyDetail.setYearPayBegin(beginMonth);
            bizcostApplyDetail.setYearPayEnd(endMonth);
        }
        bizcostApplyDetail.setAddBy(vo.getId());
        bizcostApplyDetail.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());

        List<BizcostApplyAtt> bizcostApplyAtts = new ArrayList<BizcostApplyAtt>();
        if (fileAtt1 != null) {
            for (String file : fileAtt1) {
                BizcostApplyAtt bizcostApplyAtt = createFileAtt(vo, id, file);
                bizcostApplyAtts.add(bizcostApplyAtt);
            }
        }
        if (fileAtt2 != null) {
            for (String file : fileAtt2) {
                BizcostApplyAtt bizcostApplyAtt = createFileAtt(vo, id, file);
                bizcostApplyAtts.add(bizcostApplyAtt);
            }
        }
        if (fileAtt3 != null) {
            for (String file : fileAtt3) {
                BizcostApplyAtt bizcostApplyAtt = createFileAtt(vo, id, file);
                bizcostApplyAtts.add(bizcostApplyAtt);
            }
        }
        bizcostApplyService.save(bizcostApply, bizcostApplyDetail, bizcostApplyAtts);
        LogUtil.bizLog(LogUtil.LogModelType.PROJECT, LogUtil.AcctionType.ADD,
            "商务管理 -确权支付-新增商务支付申请单：项目名=" + bizcostApply.getProName(), null);
        return RspVo.getSuccessResponse();
    }

    /**
     * 根据id查询设备合同详细  > 注意：方法命名必须为：toDealOrDetail
     *
     * @param id 投建项目id
     * @return
     * @see
     */
    @RequestMapping("/toDealOrDetail")
    public ModelAndView queryById(HttpServletRequest request, String id, String processDealWith) {
        Map<String, Object> map = new HashMap<String, Object>();
        BizcostApplyVo bizcostApply = bizcostApplyService.queryById(id);
        map.put("bizcostApply", bizcostApply);
        // 审批flag
        request.setAttribute("processDealWith", processDealWith);
        // 审批data
        processData(request, bizcostApply.getProcessId());
        return new ModelAndView("bpa/lay-bizcostApply-detail", map);
    }

    private BizcostApplyAtt createFileAtt(AuthorizationVO vo, String bizcostApplyId, String file) {
        //附件信息数组
        String[] fileAttStr = file.split("\\|");
        BizcostApplyAtt fileAtt = new BizcostApplyAtt();
        if(StringUtils.isNotEmpty(fileAttStr[2])){
            fileAtt.setId(fileAttStr[2]);
        }else{
            fileAtt.setId(IdFactory.getUUIDSerialNumber());
        }
        fileAtt.setTypes(fileAttStr[0]);
        fileAtt.setApplyId(bizcostApplyId);//项目id
        String fileName = fileAttStr[1].substring(0, fileAttStr[1].lastIndexOf("."));
        String suffix = fileAttStr[1].substring(fileAttStr[1].lastIndexOf("."),
            fileAttStr[1].length()).toLowerCase();//后缀转小写
        fileAtt.setFileName(fileName);//文件名
        fileAtt.setFileSuffix(suffix);//文件后缀
        fileAtt.setFilePath(fileAttStr[3]);//文件保存路径
        fileAtt.setAddBy(vo.getId());//添加人
        fileAtt.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());//添加时间
        return fileAtt;
    }

    /**
     * 上传附件
     * @param request
     * @param response
     * @return 
     * @throws IOException 
     * @see
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping("/uploadAtt")
    public @ResponseBody RspVo uploadAtt(HttpServletRequest request)
        throws IOException {
        List<String> returnFileStrList = new ArrayList<>();
        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest)request;
        Map<String, MultipartFile> map = multipartHttpServletRequest.getFileMap();
        if (map != null) {
            for (String name : map.keySet()) {
                MultipartFile file = map.get(name);
                if (file == null) {
                    return ErrorCodeEnum.ERROR_COMMON_PARAM.getRspVo("上传文件不能为空");
                }
                //资料类型
                String types = request.getParameter("types");
                if (StringUtils.isEmpty(types)) {
                    return ErrorCodeEnum.ERROR_COMMON_PARAM.getRspVo("缺少参数");
                }
                // 上传文件
                String fileId = IdFactory.getUUIDSerialNumber();
                // 服务器资源路径
                String serverPath = "bizcostApplyAtt";
                String filePath = FileHelper.upload(file, serverPath);
                returnFileStrList.add(types + "|" + file.getOriginalFilename().trim() + "|"
                                      + fileId + "|" + filePath);
                LogUtil.bizLog(LogModelType.PROJECT, AcctionType.ADD, "上传附件-文件名:" + filePath
                                                                      + "，文件路径:" + filePath, null);
            }
        }
        return RspVo.getSuccessResponse(returnFileStrList);
    }

    /**
     * 商务支付申请单-复制单据
     * @return
     */
    @RequestMapping("/toCopy")
    public String toCopy(HttpServletRequest request) {
        String id = request.getParameter("id");
        BizcostApplyVo bizcostApply = bizcostApplyService.queryById(id);
        request.setAttribute("bizcostApply", bizcostApply);
        return "bpa/lay-bizcostApply-copy";
    }

}
